#ifndef __FRAME_BUFFER_H__
#define __FRAME_BUFFER_H__
#include <vector>
#include <array>
#include "vec2.h"

// Framebuffer类保存着每一帧生成图像的颜色值和深度值
class Framebuffer {
public:
    Framebuffer(int w = 800, int h = 720, const Color4u8& clearColor = Color4u8{255, 138, 125, 123})
        : mWidth(w), mHeight(h) {
        mColorBuffer.resize(mWidth * mHeight, clearColor);
        mZBuffer.resize(mWidth * mHeight, 1.0f);
    }

    Color4u8 getPixelCurrentColor(const Vec2i& p) const {
        return mColorBuffer[p.x + p.y * mWidth];
    }
    Color4u8& getPixelCurrentColor(const Vec2i& p) {
        return mColorBuffer[p.x + p.y * mWidth];
    }

    float getPixelCurrentZ(const Vec2i& p) const {
        return mZBuffer[p.x + p.y * mWidth];
    }
    float& getPixelCurrentZ(const Vec2i& p) {
        return mZBuffer[p.x + p.y * mWidth];
    }

    const unsigned char* getPixelColorPtr() const {
        return mColorBuffer.data()->data.data();
    }

    void clear(const Color4u8& clearColor = Color4u8{255, 138, 125, 123}) {
        std::fill(mColorBuffer.begin(), mColorBuffer.end(), clearColor);
        std::fill(mZBuffer.begin(), mZBuffer.end(), 1.0f);
    }
private:
    std::vector<Color4u8> mColorBuffer;
    std::vector<float> mZBuffer;
    int mWidth, mHeight;
};


#endif